package equipeAgentes;

import java.awt.Color;
import java.io.IOException;

import robocode.BulletHitEvent;
import robocode.BulletMissedEvent;
import robocode.Droid;
import robocode.MessageEvent;
import robocode.TeamRobot;
import static robocode.util.Utils.normalRelativeAngleDegrees;

public class Minion extends TeamRobot implements Droid {

	private boolean isTracking;

	private double targetEnergy;

	private String targetName;

	public void run() {

		setColors(new Color(55, 55, 55), Color.blue, Color.blue);
		out.println("What is thy bidding my master?");

	}

	public void onMessageReceived(MessageEvent event) {

		String sender = event.getSender();

		if(this.isTeammate(sender)) {

			if(!this.isTracking) {

				if(event.getMessage() instanceof TeamMessage) {

					TeamMessage message = (TeamMessage) event.getMessage();
					isTracking = true;
					targetName = message.getEnemyName();
					System.out.println("I have you now " + targetName);

					double x = message.getX() - this.getX();
					double y = message.getY() - this.getY();
					// Calculate angle to target
					double theta = Math.toDegrees(Math.atan2(x, y));

					// Turn gun to target
					turnGunRight(normalRelativeAngleDegrees(theta - getGunHeading()));
					// Fire hard!

					fire(3);

					try {
						message.setTracked(true);
						broadcastMessage(message);
					} catch (IOException ex) {
						out.println("Unable to send order: ");
						ex.printStackTrace(out);
					}

				}

			}else {
				
				fire(3.0);
				
			}

		}

	}

	public void onBulletHit(BulletHitEvent event) {

		double energy = event.getEnergy();

		if(energy <= 0) {
			System.out.println("");
			this.isTracking = false;
		}

	}

	public void onBulletMissed(BulletMissedEvent event) {		
		this.isTracking = false;		
	}

}
